#include <p16F887.inc>
	__CONFIG    _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	__CONFIG    _CONFIG2, _WRT_OFF & _BOR21V

     cblock     0x20
        Delay1
        Delay2
        Display        
        LastStableState     ; keep track of switch state (open-1; closed-0)
        Counter
        Flag
     endc

     org 0
Start:
     bsf       STATUS,RP0          ; select Register Bank 1
     movlw     0xFF
     movwf     TRISA               ; Make PortA all input
     clrf      TRISD               ; Make PortD all output
     movlw     0x01
     movwf     TRISB               ; Make RBO pin input (switch)
     bsf       STATUS,RP1          ; select Register Bank 3
     movlw     0x1F
     movwf     ANSEL               ; PortA pins are all analog, PortE pins are digital
     movlw     0x00
     movwf     ANSELH              ; PortB pins are digitial (important as RB0 is switch)
     bcf       STATUS,RP0          ; address Register Bank 0
     bcf       STATUS,RP1
     clrf      PORTD               ; init LEDs to all off

     clrf      Display
     movlw     1
     movwf     LastStableState     ; Assume the Switch is up.
     clrf      Counter
     clrf      Flag
MainLoop:

     clrw                          ; assume it's not, so clear
     btfss     PORTB,0             ; wait for switch to go low
     bsf       Flag,0
     btfsc     PORTB,0
     bsf       Flag,1
     movfw     Flag
     xorlw     .3
     btfsc     STATUS,Z
     goto      AvoidEdge
     bcf       Flag,1
     goto      MainLoop

IncrementDisplay:                  ;increment the display counter and move to
                                   ;PORTD for display
     incf      Display,f           
     movf      Display,w           
     movwf     PORTD              
     clrf      Flag
    goto       MainLoop

AvoidEdge:                         ;don't set the bounce detected flag until
                                   ;after checking to make sure the switch has
                                   ;remained down (prevents false postive when
                                   ;the pushbutton is released)
    decfsz     Delay1,f
    goto       AvoidEdge
    decfsz     Delay2,f
    goto       AvoidEdge
    btfss      PORTB,0
    goto       IncrementDisplay
    goto       MainLoop

    end
